//
//  UploadTransferViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/7/10.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "UploadTransferViewController.h"
#import "AppDelegate.h"
#import "KJSRequest.h"
@interface UploadTransferViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,weak) IBOutlet UIButton *addFileBtn;

@property (strong, nonatomic) UIImagePickerController *imagePC;
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (nonatomic, strong) NSData *imageData;

@property (nonatomic,weak) IBOutlet UILabel *noImageLab;
@property (nonatomic,weak) IBOutlet UILabel *tipLab;

@property (nonatomic,weak) IBOutlet UIButton *uploadBtn;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topDis;
@end

@implementation UploadTransferViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"上传转帐凭证"];
    
    self.uploadBtn.layer.cornerRadius = 15;
    [self.uploadBtn addTarget:self action:@selector(UploadImage) forControlEvents:UIControlEventTouchUpInside];
    
    self.addFileBtn.layer.cornerRadius = 50/2;
    self.addFileBtn.layer.masksToBounds = YES;
    
    [self.addFileBtn addTarget:self action:@selector(getIdImage) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.ifAdjustContrain) {
        self.topDis.constant = self.navigationController.navigationBar.frame.size.height + 20;
    }else{
        self.topDis.constant = 0;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"orderId=%@",self.orderId);
    [super viewWillAppear:animated];
    NSLog(@"url=%@",self.imageUrl);
    if (self.imageUrl){
        UIImage *imgFromUrl=[[UIImage alloc]initWithContentsOfFile:self.imageUrl];
        [self.imageV setImage:imgFromUrl];
        [self.tipLab setHidden:YES];
        [self.noImageLab setHidden:YES];
    }
}
#pragma mark - custom method
-(void)UploadImage
{
    if ([self.imageUrl isEqualToString:@""]) {
        [GUATool showDialog:@"未添加图片" message:nil waitingTime:1.5];
    }else if (CurentDelegate.account) {
        [KJSRequest submitOfflinePaymentVoucherWithPartyId:CurentDelegate.account.partyId orderId:self.orderId uploadedFile:self.imageData _uploadedFile_fileName:[NSString stringWithFormat:@"Voucher%@.jpg",self.orderId] _uploadedFile_contentType:@"jpg" dateResourceId:@"" fileExtensionId:@"jpg" ret:^(BasicResult *model) {
            NSLog(@"返回的model＝%@",model);
            if (model.success) {
                [GUATool showDialog:@"转账凭证上传成功" message:nil waitingTime:1.5];
                if (self.uploadeSuccess){
                    self.uploadeSuccess();
                }
                [self dismiss:YES];
            }
        }];
    }else
    {
        [GUATool showDialog:@"请登录" message:nil waitingTime:1.5];
    }
}
-(void)getIdImage
{
    self.imagePC = [[UIImagePickerController alloc] init];
    [self.imagePC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    self.imagePC.delegate = self;
    [self presentViewController:self.imagePC animated:YES completion:nil];
    
}

- (NSData *)dataWithImage:(UIImage *)image {
    NSData* imageData=UIImageJPEGRepresentation(image, 0.6);
    if (imageData == nil) {
        return UIImagePNGRepresentation(image);
    }
    return imageData;
}
#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    if (picker == self.imagePC) {
        UIImage *image  = [info objectForKey:UIImagePickerControllerOriginalImage];//UIImagePickerControllerEditedImage
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *documentsDirectory=[paths objectAtIndex:0];
        NSString *savedImagePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"paymentVoucher%@.jpg",self.orderId]];
        [[self dataWithImage:image] writeToFile:savedImagePath atomically:YES];
        
        self.imageUrl = savedImagePath;
        
        self.imageData = [self dataWithImage:image];
        [self.imageV setImage:image];
        
        [self.noImageLab setHidden:YES];
        [self.tipLab setHidden:YES];
        self.addSuccess(self.imageData,self.imageUrl);
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
-(long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
@end
